//
//  EditStoreController.m
//  oc1
//
//  Created by kokuma on 2017/2/19.
//  Copyright © 2017年 kokumams. All rights reserved.
//

#import "EditStoreController.h"
#import "NSObject+Common.h"
#import "FMDBUtils.h"
//#import "GPUImageOpenGLESContext.h"
 
//#import "GLProgram.h"

@interface EditStoreController () <UITextFieldDelegate>
@property (readwrite, nonatomic, strong) NSString  *myTitle, *content ;
@property (nonatomic, strong) UITextField *txtField;
@property (nonatomic, strong) UITextView *txtView;

@end

@implementation EditStoreController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.txtField];
    [self.view addSubview:self.txtView];
    UIButton *commitBtn = [[UIButton alloc]initWithFrame:CGRectMake(120, 550, 120, 50)];
    commitBtn.backgroundColor = [UIColor yellowColor];
    [commitBtn setTitle:@"保存" forState:UIControlStateNormal];
    [commitBtn setTitle:@"保存" forState:UIControlStateHighlighted];
    [commitBtn addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    
    UIButton *showBtn = [[UIButton alloc]initWithFrame:CGRectMake(120, 610, 120, 50)];
    showBtn.backgroundColor = [UIColor yellowColor];
    [showBtn setTitle:@"show" forState:UIControlStateNormal];
    [showBtn setTitle:@"show" forState:UIControlStateHighlighted];
    [showBtn addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    //[UIButton buttonWithStyle:StrapSuccessStyle andTitle:@"点我点我" andFrame:CGRectMake(100, 60, 100, 50) target:self action:@selector(sendActivate)];
    [self.view addSubview:showBtn];
    [self initData];
}

- (void)initData{
    FMDBUtils *fmdb= [FMDBUtils sharedFMDB];
    BOOL succ= [fmdb openOrCreateDB];
    if(succ){
        NSMutableArray *queryData =[fmdb queryEditData];
        NSString *txt = @"没有数据";
        if(queryData){
            if(queryData[1]){
                _txtField.text = queryData[1];
            }
            if(queryData[2]){
                _txtView.text = queryData[2];
            }
        }
    }
    [fmdb closeDb];
}

- (void)show{
    FMDBUtils *fmdb= [FMDBUtils sharedFMDB];
    BOOL succ= [fmdb openOrCreateDB];
    if(succ){
       NSMutableArray *queryData =[fmdb queryEditData];
        NSString *txt = @"没有数据";
        if(queryData){
            txt=@"";
            if(queryData[1]){
                //[txt stringByAppendingString:@"title："];
                //[txt stringByAppendingString:queryData[1]];
                //[txt stringByAppendingString:@"; "];
               txt = [NSString stringWithFormat:@"%@%@；", @"查询：", queryData[1]];
            }
            if(queryData[2]){
                //[txt stringByAppendingString:@"content："];
               // [txt stringByAppendingString:queryData[2]];
                txt = [NSString stringWithFormat:@"%@%@",txt, queryData[2]];
            }
        }
        [NSObject showHudTipStr:txt];

    }
       [fmdb closeDb];
}

- (void)commit{
    _myTitle= _txtField.text;
    _content=_txtView.text;
    //文本
    if(!_myTitle){
        _myTitle = @"kokuma";
    }
    if(_content==nil){
        _content = @"kokumahhhh";
    }
    //DB
    FMDBUtils *fmdb= [FMDBUtils sharedFMDB];
    BOOL succ= [fmdb insertOrUpdateEditData:_myTitle mycontent:_content];
   // [NSObject showHudTipStr:[NSString stringWithFormat:@"%@,%@", @"您输入了：", txt]];
    [fmdb closeDb];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
   // NSString *txt = textField.text;
    _myTitle= _txtField.text;
    // 密码成为第一响应者
    //[self.pwdText becomeFirstResponder];
    // 关闭键盘
    //        [self.view endEditing:YES];
    // 让密码文本框关闭键盘
    //        [textField resignFirstResponder];
    [NSObject showHudTipStr:[NSString stringWithFormat:@"%@,%@", @"您输入了：", _myTitle]];
    return YES;
}

- (UITextField *) txtField{
    _txtField = [[UITextField alloc] initWithFrame:CGRectMake(0, 80, 300, 150)];
    _txtField.delegate = self;
    _txtField.backgroundColor = [UIColor whiteColor];
    _txtField.font = [UIFont systemFontOfSize:14];
    return _txtField;
}

- (UITextView *) txtView{
    _txtView = [[UITextView alloc] initWithFrame:CGRectMake(0, 250, 300, 300)];
    _txtView.delegate = self;
    _txtView.backgroundColor = [UIColor whiteColor];
    _txtView.font = [UIFont systemFontOfSize:14];
    return _txtView;
}

@end
